<template>
<div>
    <el-breadcrumb class="breadcrumb" separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path: '/users'}">用户管理</el-breadcrumb-item>
    </el-breadcrumb>
<!--  卡片视图-->
  <el-card>
    <el-row :gutter="40">
      <el-col :span="11">
        <el-input
            disabled
            clearable
            placeholder="请输入内容"
            class="input-with-select">
          <template #append>
            <el-button icon="el-icon-search"></el-button>
          </template>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary">添加用户</el-button>
      </el-col>
    </el-row>
  </el-card>
<!--  用户列表-->
  <el-table :data="userlist" border stripe>
    <el-table-column
        type="index"
        label="#"
        width="60">
    </el-table-column>
    <el-table-column
        prop="city"
        label="城市"
        width="100">
    </el-table-column>
    <el-table-column
        label="身高"
        width="90">
<!--      作用域插槽-->
      <template v-slot="scope">
        {{scope.row.height===''?'暂无':scope.row.height}}
      </template>
    </el-table-column>
    <el-table-column
        prop="realName"
        label="名字"
        width="180">
    </el-table-column>
    <el-table-column
        prop="type"
        label="类型"
        width="100">
    </el-table-column>
    <el-table-column
        label="操作" width="200px">
      <template v-slot="scope">
        <el-tooltip effect="dark" content="编辑" placement="top">
          <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
        </el-tooltip>
        <el-button size="mini" type="warning" icon="el-icon-setting"></el-button>
        <el-button size="mini" type="danger" icon="el-icon-delete"></el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[4, 5, 10, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
  </el-pagination>
</div>
</template>

<script>
export default {
  name: "users",
  data(){
    return{
      userlist: [],
      total: 0,
      pagesize: 10,
      pagenum: 1
    }
  },
  created() {
    this.getUserList()
  },
  methods:{
    async getUserList() {
      this.userlist = []
      // https://route.showapi.com/126-2?showapi_appid=728242&showapi_sign=e58b3ff6ab2441f69a32f5fedfc7faca
      console.log(localStorage.getItem('userList'))
      if(localStorage.getItem('userList')===null){
        console.log("hello")
        const res = await this.$axios.get('https://route.showapi.com/126-2?showapi_appid=728242&showapi_sign=e58b3ff6ab2441f69a32f5fedfc7faca')
        const totaluserlist = res.data.showapi_res_body.pagebean.contentlist
        this.total = res.data.showapi_res_body.pagebean.contentlist.length
        localStorage.setItem("userList",JSON.stringify(totaluserlist))
        localStorage.setItem('total',this.total)
        for(var j = (this.pagenum-1)*this.pagesize; j<(this.pagenum)*this.pagesize; j++){
          this.userlist[j-(this.pagenum-1)*this.pagesize] = totaluserlist[i]
        }
      }
      else{
        console.log("world")
        const totaluserlist = JSON.parse(localStorage.getItem('userList'))
        console.log(totaluserlist)
        this.total = parseInt(localStorage.getItem('total'))
        console.log(this.total)
        for(var i = (this.pagenum-1)*this.pagesize; i<(this.pagenum)*this.pagesize; i++){
          this.userlist[i-(this.pagenum-1)*this.pagesize] = totaluserlist[i]
        }
      }
    },
    handleSizeChange(val){
      // console.log(`每页 ${val} 条`);
      this.pagesize = val
      this.getUserList()
    },
    handleCurrentChange(val){
      // console.log(`当前页: ${val}`)
      this.pagenum = val
      console.log(this.pagenum)
      this.getUserList()
    }
  }

}
</script>

<style lang="less" scoped>
.breadcrumb{
  margin-bottom: 25px;
}
</style>
